<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Content
 *
 * @author ananda
 */
class Model_Message extends Model
{
    public $self_name = 'Message';

    protected $list;
    protected $index;

    public function init()
    {
        $this->list  = new Model_Message_ContentList();
        $this->index = new Model_Message_ContentIndex();
    }

    /**
     * 获取消息列表
     * @param <integer> $id
     * @return <string>
     */
    public function getOne($id)
    {
        $row = $this->list->getOne($id);
        if (Error::is($row)) {
            throw new Exception($row->getMessage());
        }
        return $row;
    }

    /**
     * 获取消息列表
     * @param <string> $people
     * @param <integer> $page
     * @param <integer> $limit
     * @return <string>
     */
    public function getList($people, $page, $limit)
    {
        // 取索引数据
        // 别忘记设置PEOPLE
        $this->index->setCategory($people);
        $it = $this->index->order($page, $limit);
        if (Error::is($it)) {
            throw new Exception($it->getMessage);
        }
        $id_list = array();
        foreach ($it as $row) {
            $id_list[] = $row['id'];
        }

        // 取实体数据
        $it   = $this->list->getList($id_list);
        if (Error::is($it)) {
            throw new Exception($it->getMessage());
        }
        $data = array();
        foreach ($it as $row) {
            $data[] = $row;
        }
        return $data;
    }

    /**
     * 获取所有的
     * @param <string> $people
     * @return <integer>
     */
    public function getTotal($people)
    {
        $this->index->setCategory($people);
        return $this->index->lastIndexId();
    }

    /**
     * 添加一条新的内容
     * @param <string> $people
     * @param <string> $sender
     * @param <string> $category
     * @param <integer> $createdat
     * @param <string> $data
     * @param <array> $follower
     * @return <integer>
     */
    public function post($people, $sender_id, $sender, $category, $createdat, $data, $follower_it = array())
    {
        // 先往主数据层中插入信息实体
        $row = array(
            'people'       => $people,
            'sender_id'    => $sender_id,
            'sender'       => $sender,
            'category'     => $category,
            'createdat'    => $createdat,
            'data'         => $data,
        );

        $id = $this->list->append($row);
        if (Error::is($id)) {
            throw new Exception($id->getMessage());
        }

        // 然后分别建立信息索引
        // 推送给FOLLOWER
        foreach ($follower_it as $follower) {
            $this->buildIndex($follower, $id);
        }

        return $id;
    }

    /**
     * 创建索引
     * @param <string> $people
     * @param <integer> $id
     */
    public function buildIndex($people, $id)
    {
        $this->index->setCategory($people);
        $result = $this->index->append($id);
        if (Error::is($result)) {
            throw new Exception($result->getMessage());
        }
    }
}
